package com.iwomy.envpro.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.iwomy.envpro.platform.VO.AirRealTimeDataVO;
import com.iwomy.envpro.platform.entity.AirRealTimeDataEntity;
import com.iwomy.envpro.platform.entity.AirSiteEntity;
import com.iwomy.envpro.platform.mapper.AirRealTimeDataMapper;
import com.iwomy.envpro.platform.service.AirRealTimeDataService;
import com.iwomy.envpro.platform.service.AirSiteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 空气实时数据表
 *
 * @author pig
 * @date 2023-08-31 09:28:08
 */
@Service
public class AirRealTimeDataServiceImpl extends ServiceImpl<AirRealTimeDataMapper, AirRealTimeDataEntity> implements AirRealTimeDataService {
	@Resource
	private AirSiteService airSiteService;

	@Override
	public Page getAirRealTimeList(Page page, AirRealTimeDataEntity airRealTimeData) {
		Page airSiteList = airSiteService.getAirSiteList(page, airRealTimeData.getSiteId());
		List<AirSiteEntity> records = airSiteList.getRecords();
		List<Long> siteIdList = records.stream().map(AirSiteEntity::getId).collect(Collectors.toList());
		airRealTimeData.setSiteId(null);
		//多表查询
		MPJLambdaWrapper<AirRealTimeDataEntity> wrapper = new MPJLambdaWrapper<>(airRealTimeData);
		wrapper.selectAll(AirRealTimeDataEntity.class)
				.selectAs(AirSiteEntity::getName, AirRealTimeDataVO::getSiteName)
				.selectAs(AirSiteEntity::getLatitude, AirRealTimeDataVO::getLatitude)
				.selectAs(AirSiteEntity::getLongitude, AirRealTimeDataVO::getLongitude)
				.selectAs(AirSiteEntity::getMonitorType,AirRealTimeDataVO::getMonitorType)
				.leftJoin(AirSiteEntity.class, AirSiteEntity::getId, AirRealTimeDataEntity::getSiteId);
		if (ObjectUtil.isEmpty(siteIdList)) {
			return new Page();
		}
		wrapper.in(AirRealTimeDataEntity::getSiteId, siteIdList);
		return this.getBaseMapper().selectJoinPage(page, AirRealTimeDataVO.class, wrapper);
	}
}